package com.ysw.listener;

import com.ysw.ConfigContext;
import com.ysw.environment.EnvironmentFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @ClassName ApplicationPreparedEventListener
 * @Description ApplicationPrepareEvent 事件，实例化bean之前进行调用
 * @Author ysw
 * @Date 2021/10/28 21:34
 */
public class ApplicationPreparedEventListener extends EnvironmentFactoryBean implements SmartApplicationListener {
    private Logger logger = LoggerFactory.getLogger(ApplicationPreparedEventListener.class);

    private ConfigContext configContext;



    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("ApplicationPrepareEvent-----监听到了");
        ApplicationPreparedEvent applicationPreparedEvent = (ApplicationPreparedEvent) event;
        AnnotationConfigServletWebServerApplicationContext applicationContext = (AnnotationConfigServletWebServerApplicationContext)applicationPreparedEvent.getApplicationContext();
        ConfigContext instance = ConfigContext.getInstance(applicationContext);
        logger.info("config context instance success...");
        this.configContext = instance;
        this.configContext.instanceAllProcessor();
        this.configContext.addBeanPostProcessorToSpring();
        logger.info("processor instance success ...");
        this.configContext.initAllProcessor();

    }


    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
        return ApplicationPreparedEvent.class.isAssignableFrom(eventType);
    }

}